package com.djdg.foodies.common.entity;

import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

import static javax.persistence.TemporalType.TIMESTAMP;

/**
 * @Description: 基础实体类
 * @Auther Demon
 * @Date 2018/3/13 15:10 星期二
 */
@Getter
@Setter
@MappedSuperclass
public abstract class BaseEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", columnDefinition = "INT COMMENT '主键'", nullable = false)
    protected Long id;

    @Column(name = "create_time",columnDefinition = "DATETIME COMMENT '创建时间'", nullable = false)
    @Temporal(TIMESTAMP)
    @CreationTimestamp
    protected Date createTime;

    @Column(name = "update_time",columnDefinition = "DATETIME COMMENT '更新时间'", nullable = false)
    @Temporal(TIMESTAMP)
    @UpdateTimestamp
    protected Date updateTime;

    @Version
    @Column(name = "version",columnDefinition = "INT COMMENT '乐观锁版本号'", nullable = false)
    protected Long version;

    protected String toJsonString() {
        return JSONObject.toJSONString(this);
    }

}
